Ext.Loader.setConfig({enabled: true});
//Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux','Ext.ux.exporter':'extjs4/ux/exporter'});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager'     
]);
Ext.define('TS.controller.DataImports',{
	extend: 'Ext.app.Controller',
	
	models:['ImportProductModel','ImportCustomerModel'],
	views:['settings.DataImport'],
	init:function() {
		this.control({
			'dataimport button[action=upload]':{click:this.upload},
			'dataimport button[action=submit]':{click:this.submit}
		});
	},
	upload:function(button) {
		var me = this;
		var form = button.up().up('form').getForm();
		var gridProduct = button.up('panel').up().query('#gridProduct')[0];
		var gridCustomer = button.up('panel').up().query('#gridCustomer')[0];
        if(form.isValid()){
            form.submit({
            	clientValidation: true,
                url: 'dataimport/uploadFile.do',
                waitMsg: '正在上传并解析文件,请稍等...',
                success: function(form,action) {
                	var response = Ext.decode(action.response.responseText);
                	var storeProduct = me.parseProduct(response.o);
                	gridProduct.reconfigure(storeProduct);
                   	var storeCust = me.parseCustomer(response.o);
                   	gridCustomer.reconfigure(storeCust);
                	Ext.Msg.show({
                		title:'ThinkGSP',
                		msg:response.msg,
                		buttons:Ext.Msg.OK,
                		icon:Ext.Msg.INFO
                	});
                },
                failure: function(form, action) {
                   	var response = Ext.decode(action.response.responseText);
                	Ext.Msg.show({
                		title:'ThinkGSP',
                		msg:response.msg,
                		buttons:Ext.Msg.OK,
                		icon:Ext.Msg.ERROR
                	});
                }
            });
        };
	},
	parseProduct:function(o) {
		var products = o.products;
		var store = Ext.create('Ext.data.Store',{
			model:'TS.model.ImportProductModel'
		});
		store.loadData(products);
		return store;
	},
	parseCustomer:function(o) {
		var customers = o.customers;
		var store = Ext.create('Ext.data.Store',{
			model:'TS.model.ImportCustomerModel'
		});
		store.loadData(customers);
		return store;
	},
	submit:function(button) {
		var year = button.up().down('numberfield[name=year]').getValue();
		var period = button.up().down('numberfield[name=period]').getValue();
		var gridProduct = button.up('panel').up().query('#gridProduct')[0];
		var productStore = gridProduct.getStore();
		var gridCustomer = button.up('panel').up().query('#gridCustomer')[0];
		var customerStore = gridCustomer.getStore();
		if(productStore.getCount() == 0 && customerStore.getCount() == 0) {
			Ext.Msg.alert('ThinkGsp','记录为空，不允许提交!');
			return false;
		}
		var products = new Array(productStore.getCount());
		var i = 0;
		productStore.each(function(record){
			products[i] = record.getData(false);
			i++;
		});
		i = 0;
		var customers = new Array(customerStore.getCount());
		customerStore.each(function(record){
			customers[i] = record.getData(false);
			i++;
		});
		var data = {year:year,period:period,products:products,customers:customers};
		//var data = {year:2013,period:7};
		Ext.Msg.wait('系统正在处理提交的数据，请稍等...','提交');
		Ext.Ajax.request({
			url: 'dataimport/submit.do',
			method:'POST',
			jsonData:data,
			success:function(response) {
				Ext.Msg.hide();
				var data=Ext.decode(response.responseText);
				Ext.Msg.show({
					title:'提交',
					msg:data.msg,
					buttons:Ext.Msg.OK,
					icon:Ext.Msg.INFO
				});
			},
			failure:function(response) {
				Ext.Msg.hide();
				Ext.Msg.alert('ThinkGsp','提交失败!');
			}
		});
	}
});	